package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.PageExtra;
import com.example.demo.dto.RoleDepartmentUserDTO;
import com.example.demo.request.CreateRoleDepartmentUserRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.RoleDepartmentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role-department-users")

public class RoleDepartmentUserController {

    @Autowired
    private RoleDepartmentUserService roleDepartmentUserService;


    @GetMapping
    public ResponseEntity<ApiResponse<List<RoleDepartmentUserDTO>>> getRoleDepartmentUsers(
             @RequestParam(defaultValue = "0") int page,
             @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {

        List<RoleDepartmentUserDTO> roleDepartmentUsers = roleDepartmentUserService.getRoleDepartmentUsers(page, size);
        long total = roleDepartmentUserService.countRoleDepartmentUsers();
        PageExtra extra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(roleDepartmentUsers, "请求成功", request.getRequestURI(), extra)
        );
    }


    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<RoleDepartmentUserDTO>> getRoleDepartmentUser(
            @PathVariable Integer id,
            HttpServletRequest request) {

        RoleDepartmentUserDTO roleDepartmentUser = roleDepartmentUserService.getRoleDepartmentUserById(id);
        return ResponseEntity.ok(
                ApiResponse.success(roleDepartmentUser, "查询成功", request.getRequestURI(), null)
        );
    }


    @PostMapping
    public ResponseEntity<ApiResponse<RoleDepartmentUserDTO>> createRoleDepartmentUser(
            @Valid @RequestBody CreateRoleDepartmentUserRequest request,
            HttpServletRequest requestObj) {

        com.example.demo.entity.RoleDepartmentUser roleDepartmentUser = roleDepartmentUserService.createRoleDepartmentUser(request);
        RoleDepartmentUserDTO dto = RoleDepartmentUserDTO.fromEntity(roleDepartmentUser);
        return ResponseEntity.status(201).body(
                ApiResponse.created(dto, "角色部门用户关联创建成功", requestObj.getRequestURI())
        );
    }


    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<RoleDepartmentUserDTO>> updateRoleDepartmentUser(
            @PathVariable Integer id,
            @Valid @RequestBody CreateRoleDepartmentUserRequest request,
            HttpServletRequest requestObj) {

        roleDepartmentUserService.updateRoleDepartmentUser(id, request);
        RoleDepartmentUserDTO roleDepartmentUser = roleDepartmentUserService.getRoleDepartmentUserById(id);
        return ResponseEntity.ok(
                ApiResponse.success(roleDepartmentUser, "角色部门用户关联更新成功", requestObj.getRequestURI(), null)
        );
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteRoleDepartmentUser(
            @PathVariable Integer id,
            HttpServletRequest requestObj) {

        roleDepartmentUserService.deleteRoleDepartmentUser(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "角色部门用户关联删除成功", requestObj.getRequestURI(), null)
        );
    }
    

    @GetMapping("/search/by-role-name")
    public ResponseEntity<ApiResponse<List<RoleDepartmentUserDTO>>> searchByRoleName(
             @RequestParam String roleName,
            HttpServletRequest request) {
        
        List<RoleDepartmentUserDTO> roleDepartmentUsers = roleDepartmentUserService.searchByRoleName(roleName);
        return ResponseEntity.ok(
                ApiResponse.success(roleDepartmentUsers, "请求成功", request.getRequestURI(), null)
        );
    }
    

    @GetMapping("/search/by-department-name")
    public ResponseEntity<ApiResponse<List<RoleDepartmentUserDTO>>> searchByDepartmentName(
             @RequestParam String departmentName,
            HttpServletRequest request) {
        
        List<RoleDepartmentUserDTO> roleDepartmentUsers = roleDepartmentUserService.searchByDepartmentName(departmentName);
        return ResponseEntity.ok(
                ApiResponse.success(roleDepartmentUsers, "请求成功", request.getRequestURI(), null)
        );
    }
    

    @GetMapping("/search/by-user-name")
    public ResponseEntity<ApiResponse<List<RoleDepartmentUserDTO>>> searchByUserName(
             @RequestParam String userName,
            HttpServletRequest request) {
        
        List<RoleDepartmentUserDTO> roleDepartmentUsers = roleDepartmentUserService.searchByUserName(userName);
        return ResponseEntity.ok(
                ApiResponse.success(roleDepartmentUsers, "请求成功", request.getRequestURI(), null)
        );
    }
}